﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;

namespace StockTools.Model
{
    public class SellingResultModel : BindableBase
    {
        private int sellNumber;
        private decimal sellPrice;
        private decimal amount;
        private decimal brokerage;
        private decimal transferMoney;
        private decimal stampDuty;
        private decimal totalFee;
        private decimal amountActually;
        private decimal realRate;
        private decimal income;
        private Color realRateColor;

        /// <summary>
        /// 卖出数量
        /// </summary>
        public int SellNumber { get => sellNumber; set => SetProperty(ref sellNumber, value); }
        /// <summary>
        /// 卖出价格
        /// </summary>
        public decimal SellPrice { get => sellPrice; set => SetProperty(ref sellPrice, value); }
        /// <summary>
        /// 交易金额
        /// </summary>
        public decimal Amount { get => amount; set => SetProperty(ref amount, value); }
        /// <summary>
        /// 佣金
        /// </summary>
        public decimal Brokerage { get => brokerage; set => SetProperty(ref brokerage, value); }

        /// <summary>
        /// 过户费
        /// </summary>
        public decimal TransferMoney { get => transferMoney; set => SetProperty(ref transferMoney, value); }
        /// <summary>
        /// 印花税
        /// </summary>
        public decimal StampDuty { get => stampDuty; set => SetProperty(ref stampDuty, value); }

        /// <summary>
        /// 总手续费
        /// </summary>
        public decimal TotalFee { get => totalFee; set => SetProperty(ref totalFee, value); }
        /// <summary>
        /// 实收金额
        /// </summary>
        public decimal AmountActually { get => amountActually; set => SetProperty(ref amountActually, value); }
        /// <summary>
        /// 实收益率
        /// </summary>
        public decimal RealRate { get => realRate; set => SetProperty(ref realRate, value); }
        /// <summary>
        /// 盈亏金额
        /// </summary>
        public decimal Income
        {
            get => income; set
            {
                SetProperty(ref income, value);
                if (Income > 0)
                {
                    RealRateColor = Brushes.Red.Color;
                }
                else if (Income < 0)
                {
                    RealRateColor = Brushes.Green.Color;
                }
                else
                {
                    RealRateColor = Brushes.Black.Color;
                }
            }
        }
        public string Code { get; set; }
        public Color RealRateColor { get => realRateColor; set => SetProperty(ref realRateColor, value); }

        internal void Compute(BuyingDataModel buyingData, RateSettingModel rateSetting)
        {
            if (SellNumber == 0) return;

            Amount = Math.Round(this.SellPrice * this.SellNumber, 2);

            Brokerage = ComputeBrokerage(Amount, rateSetting);
            TransferMoney = ComputeTransferMoney(rateSetting);
            StampDuty = ComputeStampDuty(Amount, rateSetting);
            TotalFee = this.Brokerage + this.TransferMoney + StampDuty;

            AmountActually = Math.Round(Amount - TotalFee, 2);

            RealRate = Math.Round((AmountActually - buyingData.PayAmount) / AmountActually * 100, 4);
            Income = AmountActually - buyingData.PayAmount;
        }

        /// <summary>
        /// 计算佣金
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="rateSetting"></param>
        /// <returns></returns>
        private decimal ComputeBrokerage(decimal amount, RateSettingModel rateSetting)
        {
            var brokerage = Math.Round(amount * rateSetting.BuyBrokerageRatio * 0.001M, 2);
            if (brokerage < 5)
                brokerage = rateSetting.MinBrokerageRatio;
            return brokerage;
        }

        /// <summary>
        /// 计算过户费
        /// </summary>
        /// <param name="rateSetting"></param>
        /// <returns></returns>
        private decimal ComputeTransferMoney(RateSettingModel rateSetting)
        {
            decimal output;
            if (!this.Code.IsNullOrEmpty() && this.Code.StartsWith("6"))
            {
                output = Math.Round(rateSetting.TransferMoney / 1000 * SellNumber, 2);
                if (output < rateSetting.MinTransferMoney)
                    output = rateSetting.MinTransferMoney;
            }
            else
            {
                output = 0;
            }
            return output;
        }

        /// <summary>
        /// 计算印花税
        /// </summary>
        /// <param name="money"></param>
        /// <param name="rateSetting"></param>
        /// <returns></returns>
        private decimal ComputeStampDuty(decimal money, RateSettingModel rateSetting)
        {
            var t = money * rateSetting.StampDuty * 0.001M;
            if (t < rateSetting.MinStampDuty)
                t = rateSetting.MinStampDuty;
            return t;
        }
    }
}
